# -*- coding:UTF-8 -*-
'''
开发环境：PyCharm
创建日期：2025/8/20 11:20
作   者：新启典学苑-樱桃
描   述：

       发送post请求
       1. 表单格式的参数，参数构造成字典，使用data传参
       2. json格式的参数，参数构造成字典，使用json传参
       3. 上传文件
'''
import random

import requests


def test_addCar():
    print("输入正确的数据，添加车辆成功")
    url = "http://192.168.1.29:8088/carRental/car/addCar.action"
    carnumber = "京A" + str(random.randint(1000, 10000))
    print("车牌号为：", carnumber)
    cs = {
        "carnumber": carnumber,
        "cartype": "卡车",
        "color": "白色",
        "price": 100000,
        "rentprice": 1000,
        "deposit": 1500,
        "isrenting": 0,
        "carimg": "",
        "description": "一辆车"
    }
    r = requests.post(url, data=cs)
    print(r.text)
    print("请求头：", r.request.headers)  # 观察Content-Type application/x-www-form-urlencoded
    print(r.json())


def test_xuezhisi_login():
    url = "http://192.168.1.29:8000/api/user/login"
    cs = {
        "userName": "admin",
        "password": "123456",
        "remember": False
    }
    r = requests.post(url, json=cs)
    print(r.text)  #
    print("请求头：", r.request.headers)  # 观察Content-Type  application/json
    a = r.json()
    assert a['message'] == "成功"
    assert a['response']['userName'] == "admin"  # 校验结果


def test_baidu():
    url = "http://www.baidu.com"
    r = requests.get(url)
    print(r.text)  # html格式的文档，不是json格式的
    # print(r.json())  # 会报错 JSONDecodeError


def test_upload():
    url = "http://192.168.1.29:8088/carRental/file/uploadFile.action"
    with open("d:/1.jpeg", "rb") as f:
        cs = {
            "mf": f
        }
        '''
        :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
            ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
            or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
            defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
            to add for the file.
        '''
        r = requests.post(url, files=cs)
        print(r.text)
        print(r.json())
        print("图片在服务器上的路径：", r.json()['data']['src'])
